package servlets;

import helper.Helper;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import constants.AttributeConstants;
import constants.ListValueConstants;
import constants.ObjectConstants;
import constants.ObjectTypeConstants;
import constants.TasksConstants;
import myJDBC.myConnection;
import myJDBC.lowLevel.Objects;
import myJDBC.lowLevel.Params;
import myJDBC.lowLevel.References;

/**
 * Servlet implementation class InitOrder
 */
@WebServlet("/InitOrder")
public class InitOrder extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		int CustomerId = -1,ApartmentId=-1, OrderId = -1;
		String fio= request.getParameter("fio");
		/*create customer */
		try{
			ApartmentId=Objects.createObject(request.getParameter("apartment")," ", ObjectTypeConstants.apartment,Integer.parseInt(request.getParameter("porch")));
			CustomerId = Objects.createObject("customer "+fio, "customer "+fio+" for Internet order", ObjectTypeConstants.customer, -1);
			Params.createParamVarchNumb(AttributeConstants.FIO, CustomerId, fio);
			References.createReference(AttributeConstants.ref_country, CustomerId, Integer.parseInt(request.getParameter("country")));
			References.createReference(AttributeConstants.ref_city, CustomerId, Integer.parseInt(request.getParameter("city")));
			References.createReference(AttributeConstants.ref_district, CustomerId, Integer.parseInt(request.getParameter("district")));
			References.createReference(AttributeConstants.ref_street, CustomerId, Integer.parseInt(request.getParameter("street")));
			References.createReference(AttributeConstants.ref_house, CustomerId, Integer.parseInt(request.getParameter("house")));
			References.createReference(AttributeConstants.ref_porch, CustomerId, Integer.parseInt(request.getParameter("porch")));
			References.createReference(AttributeConstants.ref_apartment, CustomerId, ApartmentId);
		} catch (SQLException e) {
			System.out.println(e.getMessage());			
		}		
		/*create customer end*/
		
		//create order
		try{
			OrderId = Objects.createObject("Order for "+fio, "internet order for "+fio, ObjectTypeConstants.order, CustomerId);
			Params.createParamArray(AttributeConstants.order_status, OrderId, ListValueConstants.order_status_in_progress);
			References.createReference(AttributeConstants.ref_apartment, OrderId, ApartmentId);
		} catch (SQLException e) {
			System.out.println(e.getMessage());			
		}
		//create order end
		//до сих пор все работает норм	
		//tasks init
		//
		try{
			//1
			int lineTaskId = Objects.createObject(TasksConstants.put_line_task, "нужно проложить линию в квартиру", 
					ObjectTypeConstants.task_manual, OrderId);
			Params.createParamArray(AttributeConstants.manual_task_status, lineTaskId, ListValueConstants.manual_task_status_not_started);
//			Params.createParamArray(AttributeConstants.manual_task_status, lineTaskId, ListValueConstants.manual_task_status_initiated);
			References.createReference(AttributeConstants.ref_group_id, lineTaskId, ObjectConstants.internetConnEmployee);
			//References.createReference(Helper.ref_next_task, lineTaskId, -1); // тут ссылка на какойто финальный объект или чтото вроде
			//References.createReference(Helper.ref_user_id, lineTaskId, -1);
			
			//2
			int setupCardTaskId = Objects.createObject(TasksConstants.setup_cart_task, "нужно установить карту", 
					ObjectTypeConstants.task_manual, OrderId);
		//	Params.createParamArray(AttributeConstants.manual_task_status, setupCardTaskId, ListValueConstants.manual_task_status_initiated);
			Params.createParamArray(AttributeConstants.manual_task_status, setupCardTaskId, ListValueConstants.manual_task_status_not_started);
			References.createReference(AttributeConstants.ref_next_task, setupCardTaskId, lineTaskId);
			//References.createReference(Helper.ref_user_id, setupCardTaskId, -1);
			References.createReference(AttributeConstants.ref_group_id, setupCardTaskId, ObjectConstants.cardAndRoutEmployee);
			//3
			int checkFreePortTaskId = Objects.createObject("checking free port auto-task", "проверяет наличие порта", 
					ObjectTypeConstants.task_auto, OrderId);
			Params.createParamArray(AttributeConstants.auto_task_status, checkFreePortTaskId, ListValueConstants.auto_task_status_not_started);
			References.createReference(AttributeConstants.ref_next_on_yes, checkFreePortTaskId, lineTaskId);
			References.createReference(AttributeConstants.ref_next_on_no, checkFreePortTaskId, setupCardTaskId);
			Params.createParamVarchNumb(AttributeConstants.java_api, checkFreePortTaskId, "ЗДЕСЬ ПУТЬ К МЕТОДУ");
			//4
			int setupRouterTaskId = Objects.createObject(TasksConstants.setup_router_task, "нужно установить роутер в подъезд", 
					ObjectTypeConstants.task_manual, OrderId);
//			Params.createParamArray(AttributeConstants.manual_task_status, setupRouterTaskId, ListValueConstants.manual_task_status_initiated);
			Params.createParamArray(AttributeConstants.manual_task_status, setupRouterTaskId, ListValueConstants.manual_task_status_not_started);
			References.createReference(AttributeConstants.ref_next_task, setupRouterTaskId, checkFreePortTaskId);
			//References.createReference(Helper.ref_user_id, setupRouterTaskId, -1);
			References.createReference(AttributeConstants.ref_group_id, setupRouterTaskId, ObjectConstants.cardAndRoutEmployee);
			//5
			int checkRouterTaskId = Objects.createObject("checking enable router auto-task", "проверяет наличие роутера", 
					ObjectTypeConstants.task_auto, OrderId);
			
			Params.createParamArray(AttributeConstants.auto_task_status, checkRouterTaskId, ListValueConstants.auto_task_status_not_started);
			References.createReference(AttributeConstants.ref_next_on_yes, checkRouterTaskId, checkFreePortTaskId);
			References.createReference(AttributeConstants.ref_next_on_no, checkRouterTaskId, setupRouterTaskId);
			Params.createParamVarchNumb(AttributeConstants.java_api, checkRouterTaskId, "java_api");	
			//тут надо АВТОТАСКУ стартовать!!!!!!!!!!!!!!!!!
			Helper.CheckRouterInPorch(checkRouterTaskId);
		} catch (SQLException e) {
			System.out.println(e.getMessage());			
		}
		//tasks init end
		response.sendRedirect("privateOffice");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
	
	/*private void showParams(HttpServletRequest request, HttpServletResponse response) throws IOException{
		PrintWriter pw = response.getWriter();
		  pw.println("<html><head><title>" +
		  "</title>" +
		  
		  "<table border=1 align=center>\n" +
		  "<TR bgcolor=\"#8AEAF4\">\n" +
		  "<td>Parameter Name</td>"+
		  "<td>Parameter Value(s)</td>\n");
		  Enumeration Names = request.getParameterNames();
		  while(Names.hasMoreElements()) {
		  String str = (String)Names.nextElement();
		  pw.println("<tr><td>" + str + "</td><td>");
		  String[] Values = request.getParameterValues(str);
		  if (Values.length == 1) {
		  String paramValue = Values[0];
		  if (paramValue.length() == 0)
		  pw.print("<I>No Value</I>");
		  else
		  pw.print(paramValue);
		  }
		  else {
		  pw.println("<UL>");
		  for(int i=0; i<Values.length; i++) {
		  pw.println("<LI>" + Values[i]);
		  }
		  pw.println("</UL>");
		  }
		  }
		  pw.println("</td></tr></table>\n</body></html>");
	}*/

}
